package by.grsu.dz.page.sample;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.dz.app.MyFirstWebApplication;
import by.grsu.dz.dao.impl.CircleDao;
import by.grsu.dz.entity.Circle;

/**
 * "edit" page for Circle object.
 * 
 * @author dmitry_zhivushko
 * @since Nov 2, 2014
 */
public class CircleEditPage extends WebPage {

	public CircleEditPage(final Circle circle) {
		super();

		// create FORM component which required to POST data from client->server
		final Form<Circle> form = new Form<Circle>("form", new CompoundPropertyModel<Circle>(circle));
		add(form);

		// add input fields to form
		form.add(new TextField<Double>("radius"));
		form.add(new TextField<String>("color"));

		// add 'save' button
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				// retrieve instance of DAO
				final CircleDao circleDao = new CircleDao(MyFirstWebApplication.DATA_XML_FOLDER);
				// is new Circle?
				if (circle.getId() == null) {
					// execute save of the new object
					circleDao.saveNew(circle);
				} else {
					// execute update of the new object
					circleDao.update(circle);
				}
				// go to the list page
				setResponsePage(new CirclesListPage());
			}
		});

		// add 'cancel' button
		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				// go back to the list page
				setResponsePage(new CirclesListPage());
			}
		});
	}
}
